//
//  BuildingConstructionLogicSystem.m
//  projecthz
//
//  Created by YDJH on 2017/5/11.
//  Copyright © 2017年 tali. All rights reserved.
//


#define isEqualDoor(type) [type isEqualToString:WoodDoor] || [type isEqualToString:FenceDoor]|| [type isEqualToString:SecurityDoor]|| [type isEqualToString:GlassDoor]



#import "BuildingConstructionLogicSystem.h"
#import "HZConstruction.h"
#import "HZSandTable.h"
#import "HZInsideWall.h"




@interface BuildingConstructionLogicSystem ()



@end

@implementation BuildingConstructionLogicSystem

+(BuildingConstructionLogicSystem*)standardBuildingConstructionLogicSystem
{
    __strong static BuildingConstructionLogicSystem *_sharedObject = nil;
    @synchronized(self) {//同步 执行 防止多线程操作
        if (_sharedObject == nil) {
            _sharedObject = [[self alloc] init];
            //初始化数据
            _sharedObject.buildingDataArr= [BuildingData gobackBuildingModel]; //建筑物模型集合
            _sharedObject.roomDataArr = [RoomData gobackRoomModel]; //房间模型集合
            _sharedObject.furnitureDataArr = [FurnitureData gobackFurnitureModel]; // 道具模型集合
           
        }
    }
    return _sharedObject;
}

- (NSMutableArray *)buildingStructure {
    if (!_buildingStructure) {
        _buildingStructure = [NSMutableArray array];
    }
    return _buildingStructure;
}

- (NSMutableArray *)furnitureArr {
    if (!_furnitureArr) {
        _furnitureArr = [NSMutableArray array];
    }
    return _furnitureArr;
}

- (NSMutableArray *)roomTypeArr {
    if (!_roomTypeArr) {
        _roomTypeArr = [NSMutableArray array];
    }
    return _roomTypeArr;
}

- (NSMutableArray *)lobbyTypeArr {
    if (!_lobbyTypeArr) {
        _lobbyTypeArr = [NSMutableArray array];
    }
    return _lobbyTypeArr;
}

- (NSArray *)roomDataArr {
    if (!_roomDataArr) {
        _roomDataArr = [NSMutableArray array];
    }
    return _roomDataArr;
}

- (NSArray *)buildingDataArr {
    if (!_buildingDataArr) {
        _buildingDataArr = [NSMutableArray array];
    }
    return _buildingDataArr;
}

- (NSArray *)furnitureDataArr {
    if (!_furnitureDataArr) {
        _furnitureDataArr = [NSMutableArray array];
    }
    return _furnitureDataArr;
}

//- (NSMutableArray *)buildingConstructionLogicWithBuildingType:(NSString *)type{
//    NSMutableArray *building;
//    
//    NSArray *build = [BuildingData gobackBuildingModel];
//    for (BuildingData *model in build) {
//        if ([model.buildType isEqualToString:type]) {
//            self.buildingModel = model;
//            break;
//        }
//    }
//    
//    [self requestTheBuildingAllRoomTypeArr];
//    building = [self configureRoomInformation];
//    
//    return building;
//}



/*
 沙盘
 
 从建筑，房间，家具表中读取plist文件
 1.初始化显式沙盘家具
 1.1 初始化家具状态
 1.2 初始化家具指令集模型
 1.2.1 依据家具指令集生成 家具指令,指令图片，以及关联指令实现方法
 
 2.初始化隐式按钮
 2.1 初始化隐式家具的位置
 2.2 根据沙盘家具的当前状态初始化当前隐式按钮是否显示的状态
 
 3.初始化沙盘上的楼
 3.1 根据楼的类型和 规模 初始化内部结构
 3.1.1 根据楼的 类型 初始化 房间 模型 是否有大厅和地下室
 3.1.2 初始化房间基础数据
 3.1.2.1 初始化 房间外部四面墙 和 墙面图片
 3.1.2.2.1 初始化家具模型
 3.1.2.2.2 根据家具所有的状态随机生成一个为当前状态
 3.1.2.2.3 初始化家具指令模型
 3.1.2.2.3.1 依据家具指令集 生成 家具指令, 指令图片, 指令实现方法
 
 3.1.2.2 初始化 房间内部四面墙 和 墙面图片
 3.1.2.2.1 根据外部家具关联内部家具
 3.1.2.2.2 初始化房间内部包含的家具
 3.1.2.2.3 根据家具所有的状态随机生成一个为当前状态
 3.1.2.2.4 初始化家具指令模型
 3.1.2.2.4.1 依据家具指令集 生成 家具指令, 指令图片, 指令实现方法
 
 4.初始化沙盘户外
 4.1 初始化户外基础数据
 */



//生成建筑物模型
- (HZConstruction *)configureHZConstructionWith:(NSString *)type {
    
    HZConstruction *construction;
    
    BuildingData *buildingData;
    
    //获取该建筑物的模型
    for (BuildingData *model in self.buildingDataArr) {
        if ([model.buildType isEqualToString:type]) {
            buildingData = model;
            break;
        }
    }
    //赋值楼层轮廓
    self.structure = buildingData.structure;
    //获取该建筑包含的房间
    for (NSString *roomType in buildingData.insideRoomType) {
        for (RoomData *roomData in self.roomDataArr) {
            if ([roomData.type isEqualToString:roomType]) {
                [self.roomTypeArr addObject:roomData];
            }
        }
    }

    //1.初始化建筑物基础数据
    construction = [BuildingData gobackConsturctionModelWithModel:buildingData];
    //2.判断是否存在沙盘 或者 楼
    BOOL isSandTable = [BuildingData containsTSandable:buildingData];
    //3.初始化 沙盘 或者 楼
    if (isSandTable) {
        // 沙盘
        HZSandTable *sandTable = [[HZSandTable alloc] init];
        
        //显式家具
        sandTable.outsideFurniture = [FurnitureData goBackArrayWithSandTableFutiniture:buildingData.sandTableFurniture withFurData:self.furnitureDataArr];
        
        //隐式家具
        sandTable.hiddenFurniture = [FurnitureData goBackArrayWithSandTableHiddenFutiniture:buildingData.outsideRoomType withRoomData:self.roomDataArr];

        sandTable.type = buildingData.buildID;
        
        sandTable.isHiddenFurniture = [FurnitureData gobackBOOLWithSandTableFutiniture:sandTable.outsideFurniture];
        
        sandTable.picture = [NSString stringWithFormat:@"sandtable_%@",sandTable.type];
        
        sandTable.outDoor = [BuildingData gobackOutDoorWithSandTable:sandTable.hiddenFurniture withData:self.roomDataArr];
        
        sandTable.outsideBuilding = [BuildingData gobackBuildingWithSandTable:sandTable.hiddenFurniture withData:buildingData withRoomArr:self.roomDataArr];
        
        construction.sandTable = sandTable;
    } else {
        // 楼
        HZBuilding *building = [BuildingData goBackBuildingWithData:buildingData withRoomArr:self.roomDataArr];
        
        /*
         1.初始化内部结构
            1.1 根据楼的规模初始化 房间 模型
                1.1.1 初始化房间基础数据
                1.1.2 初始化房间内部四面墙
                    1.1.2.1 根据房间包含的家具类型 初始化 墙面
                        1.1.2.1.1 初始化沙盘家具
                        1.1.2.1.1 初始化家具基础数据
                        1.1.2.1.2 根据家具所有的状态随机生成一个为当前状态
                        1.1.2.1.3 初始化家具指令模型
                            1.1.2.1.3.1 根据家具指令集生成 家具指令, 指令图片, 指令实现方法
         2.根据内部结构  初始化外墙
            2.1 根据房间内部的墙面 关联 外墙
            2.2 根据规则 初始化房间 外墙 外梯
         */
        
        
        
        construction.building = building;
    }
    
    
    
    
    return construction;
}


//更新建筑
- (NSMutableArray *)updatingBuilding:(HZBuilding *)building withbuildingData:(BuildingData *)data{
    
    
    NSMutableArray *buildingStructure = [NSMutableArray array];
    
    NSArray *structure =  [data.structure componentsSeparatedByString:@","];
    NSString *floorNum = structure[0];
    NSString *roomNum = structure[1];
    
    for (int i = 0; i<floorNum.integerValue; i++) {
        NSArray *floorArr = building.buildingStructure[i];
        for (int j = 0; j<roomNum.integerValue; j++) {
            HZRoom *room = floorArr[j];
            if ([room.roomCategory isEqualToString:@"1"]) {
                room = [self configureLobby:room withBuildingData:data currentI:i currentJ:j];
            }else {
                room = [self configureRoom:room withBuildingData:data currentI:i currentJ:j];
            }
        }
    }

    return buildingStructure;
}

- (HZRoom *)configureLobby:(HZRoom *)room withBuildingData:(BuildingData *)data currentI:(int)i currentJ:(int)j{
    
    HZRoom *lobby = room;
    
    HZInsideWall *insidewall1 = [HZInsideWall new];
    
    insidewall1.wallPicture = [NSString stringWithFormat:@"insidewall1_%@",lobby.type];
    
//    HZFurniture *furniture = [[HZFurniture alloc] init];
    

    

    
    return lobby;
}

- (HZRoom *)configureRoom:(HZRoom *)room withBuildingData:(BuildingData *)data currentI:(int)i currentJ:(int)j{
    
    HZRoom *model = room;
    HZInsideWall *insidewall1 = [HZInsideWall new];
    HZInsideWall *insidewall2 = [HZInsideWall new];
    HZInsideWall *insidewall3 = [HZInsideWall new];
    HZInsideWall *insidewall4 = [HZInsideWall new];
    insidewall1.wallPicture = [NSString stringWithFormat:@"insidewall1_%@",model.type];
    insidewall2.wallPicture = [NSString stringWithFormat:@"insidewall2_%@",model.type];
    insidewall3.wallPicture = [NSString stringWithFormat:@"insidewall3_%@",model.type];
    insidewall4.wallPicture = [NSString stringWithFormat:@"insidewall4_%@",model.type];
    
    
    
    return model;
}


- (void)requestTheBuildingAllRoomTypeArr {
    //获取 该建筑中所有房间的属性
    NSArray *roomtypeArr = [RoomData gobackRoomModel];
    [self.buildingModel.insideRoomType enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *roomType = obj;
        [roomtypeArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            RoomData *model = obj;
            if ([roomType isEqualToString:model.type]) {
                [self.roomTypeArr addObject:model];
                *stop = YES;
            }
        }];
    }];
    //获取 该建筑中大厅的属性
    if (self.buildingModel.isHavaInsideLobby) {
        [self.buildingModel.buildLobby enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *roomType = obj;
            [roomtypeArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                RoomData *model = obj;
                if ([roomType isEqualToString:model.type]) {
                    [self.lobbyTypeArr addObject:model];
                    *stop = YES;
                }
            }];
        }];
    }
    //获取楼梯间
    [roomtypeArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        RoomData *model = obj;
        if ([@"19" isEqualToString:model.type]) {
            self.staircaseRoom = model;
            *stop = YES;
        }
    }];
    //获取办公室
    [roomtypeArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        RoomData *model = obj;
        if ([@"2" isEqualToString:model.type]) {
            self.officeRoom = model;
            *stop = YES;
        }
    }];

    self.furnitureArr = [FurnitureData gobackFurnitureModel];
    self.lobbyTypeArr = (NSMutableArray *)[[self.lobbyTypeArr reverseObjectEnumerator] allObjects];
}



//根据数据源的数量随机生成下标
- (NSInteger )randomNumWithCount:(NSInteger )count{
    NSInteger x = arc4random() % count;
    return x;
}

@end
